﻿' LibWowAPI
' by Ronald M. Clifford (roncli@roncli.com)
'
' This source code is released under the GNU Library General Public License (LGPL) Version 2.1.

Imports System
Imports System.Collections.ObjectModel
Imports System.Globalization
Imports System.Linq
Imports Newtonsoft.Json
Imports roncliProductions.LibWowAPI.Enums
Imports roncliProductions.LibWowAPI.Extensions

Namespace roncliProductions.LibWowAPI.PvP

    ''' <summary>
    ''' A class that retrieves arena ladder information from the Blizzard WoW API.
    ''' </summary>
    ''' <remarks></remarks>
    Public Class ArenaLadder
        Inherits WowAPIData

        Private lLadder As New Schema.ladder

#Region "WowAPIData Overrides"

        Protected Overrides ReadOnly Property URI As Uri
            Get
                QueryString.Clear()
                If Options.Teams > 0 Then
                    QueryString.Add("size", Options.Teams.ToString(CultureInfo.InvariantCulture))
                End If
                Return New Uri(String.Format(CultureInfo.InvariantCulture, "/api/wow/pvp/arena/{0}/{1}v{1}", Options.Battlegroup, Options.TeamSize), UriKind.Relative)
            End Get
        End Property

        Protected Overrides ReadOnly Property CacheKey As String
            Get
                Return String.Format(CultureInfo.InvariantCulture, "LibWowAPI.ArenaLadder.{0}.{1}.{2}", Options.Battlegroup, Options.TeamSize, Options.Teams)
            End Get
        End Property

#End Region

#Region "Properties"

        ''' <summary>
        ''' The options for looking up arena ladder information.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Options As New ArenaLadderOptions

        Private colTeams As Collection(Of Team)
        ''' <summary>
        ''' Arena ladder information as returned from the Blizzard WoW API.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Teams As Collection(Of Team)
            Get
                Return colTeams
            End Get
        End Property

#End Region

#Region "Methods"

#Region "Constructors"

        ''' <summary>
        ''' A default constructor to retrieve arena ladder information from the Blizzard WoW API.
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub New()
        End Sub

        ''' <summary>
        ''' A constructor to retrieve an arena ladder.
        ''' </summary>
        ''' <param name="strBattlegroup"></param>
        ''' <param name="intTeamSize"></param>
        ''' <remarks></remarks>
        Public Sub New(strBattlegroup As String, intTeamSize As Integer)
            Options.Battlegroup = strBattlegroup
            Options.TeamSize = intTeamSize
            Load()
        End Sub

#End Region

        ''' <summary>
        ''' Loads the arena ladder.
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub Load()
            MyBase.Retrieve()
            lLadder = CType(JsonConvert.DeserializeObject(Data, GetType(Schema.ladder)), Schema.ladder)
            colTeams = (
                From t In lLadder.arenateam
                Select New Team(
                    t.realm,
                    t.ranking,
                    t.rating,
                    t.teamsize,
                    Date.Parse(t.created, CultureInfo.InvariantCulture),
                    t.name,
                    t.gamesPlayed,
                    t.gamesWon,
                    t.gamesLost,
                    t.sessionGamesPlayed,
                    t.sessionGamesWon,
                    t.sessionGamesLost,
                    t.lastSessionRanking,
                    t.side.GetSide(),
                    t.currentWeekRanking,
                    (From m In t.members
                     Select New Member(
                         New Character(
                             m.character.name,
                             m.character.realm,
                             m.character.class.GetClass(),
                             m.character.race.GetRace(),
                             CType(m.character.gender, Gender),
                             m.character.level,
                             m.character.achievementPoints,
                             m.character.thumbnail
                             ),
                         m.rank,
                         m.gamesPlayed,
                         m.gamesWon,
                         m.gamesLost,
                         m.sessionGamesPlayed,
                         m.sessionGamesWon,
                         m.sessionGamesLost,
                         m.personalRating
                         )
                     ).ToCollection()
                 )
             ).ToCollection()
        End Sub

#End Region

    End Class

End Namespace
